package common.concurrency.baeldung.wait_notify;

import java.util.concurrent.ThreadLocalRandom;

/**
 * @author zhang hao dong
 * @date 2025-02-27 14:50
 */
@lombok.Data
public class Sender implements Runnable {
    private Data data;

    public Sender(Data data) {
        this.data = data;
    }

    @Override
    public void run() {
        System.out.println("sender is running");
        String packets[] = {"First packet", "Second packet", "Third packet", "Fourth packet", "End"};
        for (String packet : packets) {
            data.send(packet);
            // Thread.sleep() to mimic heavy server-side processing
            try {
                Thread.sleep(ThreadLocalRandom.current().nextInt(5000,7000));
                System.out.println("sender is sleep");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                System.err.println("Thread Interrupted");
            }
        }
    }
}
